from rest_framework.fields import UUIDField

from wxcloudrun.models import get_web_settings
from wxuser.models import Users


def get_web_settings_statistical_exclude_user_list() -> list:
    uid_list = get_web_settings_statistical_exclude_user_uid_list()
    if len(uid_list) > 0:
        user_list = Users.objects.filter(deleted=False).filter(id__in=uid_list).all()
        return user_list
    return []


def get_web_settings_statistical_exclude_user_uid_list() -> list:
    web_settings = get_web_settings()
    new_uid_list = []
    if (
        web_settings is not None
        and web_settings.statistical_exclude_user_id is not None
        and not web_settings.statistical_exclude_user_id.isspace()
    ):
        uid_list = web_settings.statistical_exclude_user_id.split(",")

        for uid in uid_list:
            if uid is not None and not uid.isspace() and uid.isnumeric():
                new_uid_list.append(int(uid))
    return new_uid_list


def get_web_settings_statistical_exclude_user_uuid_list() -> list:
    user_list = get_web_settings_statistical_exclude_user_list()
    user_uuid_list = []
    if len(user_list) > 0:
        for user in user_list:
            user_uuid_list.append(user.uuid)
    return user_uuid_list


def check_web_settings_statistical_exclude_user(user) -> bool:
    user_list = get_web_settings_statistical_exclude_user_list()
    user_uuid = user if isinstance(user, str) else user.uuid
    uuid_field = UUIDField()
    for user_item in user_list:
        user_item_uuid = uuid_field.to_representation(user_item.uuid)
        if user_item_uuid == user_uuid:
            return True
    return False


def check_web_settings_statistical_exclude_uid(uid) -> bool:
    uid_list = get_web_settings_statistical_exclude_user_uid_list()
    _uid = uid
    if not isinstance(uid, int):
        _uid = int(uid)
    for user_id in uid_list:
        if user_id == _uid:
            return True

    return False
